package org.chen.newsapi.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.chen.newsapi.common.Result;
import org.chen.newsapi.dto.ArticleDetailVO;
import org.chen.newsapi.dto.ArticleDocument;
import org.chen.newsapi.dto.ArticleIndex;
import org.chen.newsapi.dto.UserArticleQueryParam;
import org.chen.newsapi.entity.Article;
import org.chen.newsapi.mapper.ArticleMapper;
import org.chen.newsapi.service.ArticleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {
    
    @Autowired
    private ArticleMapper articleMapper;
    
    private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    
    @Override
    public org.chen.newsapi.common.Page<ArticleIndex> getArticles(UserArticleQueryParam param) {
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<Article> page = new Page<>(param.getPage(), param.getSize());
        IPage<Article> result = articleMapper.selectArticlesByCondition(page, param);

        List<ArticleIndex> content = result.getRecords().stream()
                .map(this::convertToArticleIndex)
                .toList();

        return org.chen.newsapi.common.Page.of(
                content,
                param.getPage(),
                param.getSize(),
                result.getTotal()
        );
    }
    
    @Override
    public Result<ArticleDetailVO> getArticleDetail(String articleId) {
        Article article = articleMapper.selectById(articleId);
        if (article == null) {
            return Result.error("文章不存在");
        }
        
        // 获取相关文章
        List<Article> relatedList = articleMapper.selectRelatedArticles(article.getCategory(), articleId);
        
        ArticleDetailVO detailVO = new ArticleDetailVO();
        detailVO.setArticle(convertToArticleDocument(article));
        detailVO.setRelatedArticles(
                relatedList.stream()
                        .map(this::convertToArticleDocument)
                        .collect(Collectors.toList())
        );
        
        return Result.success(detailVO);
    }
    
    private ArticleIndex convertToArticleIndex(Article article) {
        ArticleIndex index = new ArticleIndex();
        BeanUtils.copyProperties(article, index);
        index.setTags(convertStringToList(article.getTags()));
        index.setCreateTime(article.getCreateTime().format(formatter));
        return index;
    }
    
    private ArticleDocument convertToArticleDocument(Article article) {
        ArticleDocument document = new ArticleDocument();
        BeanUtils.copyProperties(article, document);
        document.setTags(convertStringToList(article.getTags()));
        document.setTimestamp(article.getCreateTime().format(formatter));
        document.setCreateTime(article.getCreateTime().format(formatter));
        document.setUpdateTime(article.getUpdateTime().format(formatter));
        if (document.getMainImageUrl() == null) {
            document.setMainImageUrl(article.getThumbnailUrl());
        }
        // 这里可以添加图片信息的转换逻辑
        document.setImages(Collections.emptyList()); // 暂时返回空列表
        return document;
    }
    
    private List<String> convertStringToList(String str) {
        if (str == null || str.trim().isEmpty()) {
            return Collections.emptyList();
        }
        return Arrays.asList(str.split(","));
    }
}